<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Search extends WxHomeController
{

    function __construct()
    {
        parent::__construct();
        $this->sidebar = "Information";
        $this->date = date('Y-m-d', time());
        $this->month = date('m', time());//当前学期
        // $this->month = date('m', '1661740784');
    }
    // class_name课程名；teacher_name教师名称
    function Index($type)
    {
        $data['class_name'] = '';
        $data['teacher_name'] = '';
        $data['teacher_id'] = '';
        $data['class_id'] = '';
        $data['type'] = $type;
        if (isset($_GET['class_name'])) {
            $data['class_name'] = urldecode($_GET['class_name']);
        } else {
            $data['class_name'] = '';
        }
        if (isset($_GET['class_id'])) {
            $data['class_id'] = urldecode($_GET['class_id']);
        }
        if (isset($_GET['teacher_name'])) {
            $data['teacher_name'] = urldecode($_GET['teacher_name']);
        } else {
            $data['teacher_name'] = '';
        }
        if (isset($_GET['teacher_id'])) {
            $data['teacher_id'] = urldecode($_GET['teacher_id']);
        } else {
            $data['teacher_id'] = '';
        }
        $this->load->view("home/search", $data);
    }
    // 获取查询列表数据
    function getList()
    {
        $type = $_POST['type'];
        $value = $_POST['value'];
        if ($value != '') {
            // 获取课程数据
            if ($type == SearchType::First[XPHEnum::Value]) {
                $this->load->model("Class_model");
                // $month = date('m', time());
                if ($this->month == 9 || $this->month == 10 || $this->month == 11 || $this->month == 12 || $this->month == 1) {
                    if ($this->month == 1) {
                        $year = date('Y', time()) - 1;
                        $year_last = date('Y', time());
                    } else {
                        $year = date('Y', time());
                        $year_last = date('Y', time()) + 1;
                    }
                    // $year=date('Y',time());
                    // $year_last=date('Y',time())+1;
                    $default_academic_year = strval($year . '-' . $year_last);
                } else if ($this->month == 2 || $this->month == 3 || $this->month == 4 || $this->month == 5 || $this->month == 6 || $this->month == 7 || $this->month == 8) {
                    $year = date('Y', time()) - 1;
                    $year_last = date('Y', time());
                    $default_academic_year = strval($year . '-' . $year_last);
                }
                $connar['where'] = $this->sqlLikeEscape('name like "%??%" and elective_lesson_no like "%??%"', array($value, $default_academic_year));
                $connar['items'] = "name,id";
                $connar['groupby'] = "name";
                $data = $this->Class_model->get_list_full($connar);
            } else if ($type == SearchType::Second[XPHEnum::Value]) {
                // 获取教师数据
                $this->load->model('Admin_model');
                $connar['where'] = $this->sqlLikeEscape('status=?? and (username like "%??%" or usercode like "%??%" )', array(CommonStatus::UnDeleted[XPHEnum::Value], $value, $value));
                $data = $this->Admin_model->get_list_full($connar);
            }
        }
    }


    // 0426教师信息搜索
    function teatcherIndex($attend_id)
    {
        $data['attend_id'] = $attend_id;
        $this->load->view("home/search1", $data);
    }
    // 0426教师信息搜索
    function getTeatcherList()
    {
        $this->load->model('Admin_model');
        if ($this->month == 9 || $this->month == 10 || $this->month == 11 || $this->month == 12 || $this->month == 1) {
            if ($this->month == 1) {
                $year = date('Y', time()) - 1;
                $year_last = date('Y', time());
            } else {
                $year = date('Y', time());
                $year_last = date('Y', time()) + 1;
            }
            $default_academic_year = strval($year . '-' . $year_last.'-1');
        } else if ($this->month == 2 || $this->month == 3 || $this->month == 4 || $this->month == 5 || $this->month == 6 || $this->month == 7 || $this->month == 8) {
            $year = date('Y', time()) - 1;
            $year_last = date('Y', time());
            $default_academic_year = strval($year . '-' . $year_last.'-2');
        }

        $connar['where'] = $this->sqlEscape(' status=?? and ( role_id=?? or find_in_set(??,sub_role_id))', array(CommonStatus::UnDeleted[XPHEnum::Value], WebRole::Teacher[XPHEnum::Value], WebRole::Teacher[XPHEnum::Value]));
        $connar['groupby'] = "db_admin.id";
        if (isset($_POST['teacher_code']) && $_POST['teacher_code'] != '') {
            $connar['where'] .= $this->sqlLikeEscape(' and  (username like "%??%" or usercode like "%??%" )', array($_POST['teacher_code'], $_POST['teacher_code']));
        } else {
            
            if (isset($_SESSION[SESS_ASSESS]['course_units'])) {
                $connar['items'] = "db_admin.*,c.id as cid";
                $connar['where'] .= $this->sqlEscape(' and course_units=??', array($_SESSION[SESS_ASSESS]['course_units']));
                $connar['join'] = array("db_class c", 'c.teacher_id=db_admin.usercode');
            }
            if (isset($_SESSION[SESS_ASSESS]['listen_time'])) {
                // var_dump($_SESSION[SESS_ASSESS]['listen_time']);
                $connar['items'] = "db_admin.*,c.id as cid";
                $weekarray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
                $weekday = $weekarray[date("w", strtotime($_SESSION[SESS_ASSESS]['listen_time']))];
                $quantumsw = date('H', strtotime($_SESSION[SESS_ASSESS]['listen_time']));
                $quantumsw = $quantumsw <= 12 ? '上午' : '下午';
                $connar['where'] .= $this->sqlLikeEscape(' and elective_lesson_no like "%??%"', array($default_academic_year));
                $connar['where'] .= $this->sqlLikeEscape(' and sksj1 like "%??%"', array($weekday . $quantumsw));
                $connar['join'] = array("db_class c", 'c.teacher_id=db_admin.usercode');
            }
            if (isset($_SESSION[SESS_ASSESS]['class_name'])) {
                $connar['items'] = "db_admin.*,c.id as cid";
                $connar['where'] .= $this->sqlLikeEscape(' and c.name like "%??%"', array($_SESSION[SESS_ASSESS]['class_name']));
                $connar['join'] = array("db_class c", 'c.teacher_id=db_admin.usercode');
            }
        }

        $data = $this->Admin_model->get_list_full($connar);
        foreach($data as $k=>$v){
            if(!isset($v['cid'])){
                $data[$k]['cid'] = 0;
            }
        }
        // var_dump($data);
        // echo $this->db->last_query();
        exit(retJson('返回搜索结果', true, $data));
    }
    // 0507 教师信息session保存
    function setTeacher()
    {
        $this->load->model("Class_model");
        $this->load->model("Admin_model");
        if (isset($_POST['teacher_id'])) {
            $data = $this->Admin_model->get_single(array('id' => $_POST['teacher_id']));
            $_SESSION[SESS_ASSESS]['teacher_id'] = $data['id'];
            $_SESSION[SESS_ASSESS]['teacher_name'] = $data['username'];
            $_SESSION[SESS_ASSESS]['teacher_code'] = $data['usercode'];
            $_SESSION[SESS_ASSESS]['title'] = $data['title'];
            $_SESSION[SESS_ASSESS]['age_groups'] = $data['age_groups'];
            if ($data['age']) {
                $_SESSION[SESS_ASSESS]['age'] = $data['age'];
            } else {
                $_SESSION[SESS_ASSESS]['age'] = '无';
            }

            $_SESSION[SESS_ASSESS]['teachers_attribute'] = $data['teachers_attribute'];
            $_SESSION[SESS_ASSESS]['dept_name'] = $data['dept_name'];
            $_SESSION[SESS_ASSESS]['dept_name'] = $data['dept_name'];
            $_SESSION[SESS_ASSESS]['dept_name'] = $data['dept_name'];
            if($_POST['class_id'] != 0){
                $class = $this->Class_model->get_single(array('id' => $_POST['class_id']));
                $_SESSION[SESS_ASSESS]['class_id'] = $class['id'];
                // $_SESSION[SESS_ASSESS]['classtype']=$data['type'];
                $_SESSION[SESS_ASSESS]['classname'] = $class['classname'];
                if ($class['classroom']) {
                    $_SESSION[SESS_ASSESS]['classroom'] = explode(';', $class['classroom'])[0];
                }
            }
           
            exit(retJson('保存成功', true));
        }
        exit(retJson('系统错误', false));
    }

    /*  // 0507课程信息搜索
    function classIndex($attend_id,$teacher_code=false){        
        if($teacher_code){
            $data['teacher_code']=$teacher_code;
        }else{
            $data=array();
        }
        $data['attend_id']=$attend_id;
        $this -> load ->view("home/search2",$data);
    } */
    // 202109010课程信息搜索
    function classIndex($attend_id, $course_units = false, $listen_time = false)
    {
        if ($course_units && urldecode($course_units) != '0') {
            $data['course_units'] = urldecode($course_units);
            // $_SESSION[SESS_ASSESS]['course_units']=urldecode($course_units);
        } else {
            $data = array();
        }
        if ($listen_time && urldecode($listen_time) != '0') {
            $_SESSION[SESS_ASSESS]['listen_time'] = urldecode($listen_time);
        }
		if($course_units == 'mkszy') {
            $data['course_units'] = '马克思主义学院';
        } else if($course_units == 'hjxy') {
            $data['course_units'] = '黄酒学院、纺织工程学院';
        }

        $data['listen_time'] = urldecode($listen_time) ? urldecode($listen_time) : array();
        $data['attend_id'] = $attend_id;
        $this->load->view("home/search2", $data);
    }
    // 0507课程信息搜索
    function getClassList()
    {
        $this->load->model("Class_model");
        $weekarray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

        // pri($this->month);
        // $month = date('m', time());
        if ($this->month == 9 || $this->month == 10 || $this->month == 11 || $this->month == 12 || $this->month == 1) {
            if ($this->month == 1) {
                $year = date('Y', time()) - 1;
                $year_last = date('Y', time());
            } else {
                $year = date('Y', time());
                $year_last = date('Y', time()) + 1;
            }
            $default_academic_year = strval($year . '-' . $year_last.'-1');
        } else if ($this->month == 2 || $this->month == 3 || $this->month == 4 || $this->month == 5 || $this->month == 6 || $this->month == 7 || $this->month == 8) {
            $year = date('Y', time()) - 1;
            $year_last = date('Y', time());
            $default_academic_year = strval($year . '-' . $year_last.'-2');
        }

        $connar['where'] = $this->sqlLikeEscape('elective_lesson_no like "%??%" and db_admin.status=1', array($default_academic_year));
        if (isset($_POST['class_name']) && $_POST['class_name'] != '') {
            $connar['where'] .= $this->sqlLikeEscape(' and name like "%??%"', array($_POST['class_name']));
            $connar['orderby'] ="db_class.name asc,db_admin.usercode desc";
        }
        if (isset($_SESSION[SESS_ASSESS]['teacher_code']) && $_SESSION[SESS_ASSESS]['teacher_code'] != '') {
            $connar['where'] .= $this->sqlLikeEscape(' and teacher_id=??', array($_SESSION[SESS_ASSESS]['teacher_code']));
        }
        if (isset($_POST['teacher_code']) && $_POST['teacher_code'] != '') {
            $connar['where'] .= $this->sqlLikeEscape(' and teacher_id=??', array($_POST['teacher_code']));
        }
        if (isset($_POST['course_units']) && $_POST['course_units'] != '') {
            // $connar['where'].=$this->sqlLikeEscape(' and course_units="??"',array($_POST['course_units']));
            $connar['where'] .= " and course_units like '%" . $_POST['course_units'] . "%'";
        }

        if (isset($_POST['listen_time']) && $_POST['listen_time'] != '') {
            // var_dump($_POST['listen_time']);
            $weekday = $weekarray[date("w", strtotime($_POST['listen_time']))];
            $quantumsw = date('H', strtotime($_POST['listen_time']));
            $quantumsw = $quantumsw <= 12 ? '上午' : '下午';
            $connar['where'] .= $this->sqlLikeEscape(' and sksj1 like "%??%"', array($weekday . $quantumsw));
        }
        $connar['join'] = array('db_admin', 'db_admin.usercode=db_class.teacher_id');
        $connar['items'] = "db_class.id,name,usercode,username,db_admin.dept_name,sksj,db_class.classroom,db_class.classname";
        $connar['groupby'] = "db_class.id";
      
        $data = $this->Class_model->get_list_full($connar);
        // echo $this->db->last_query();

        foreach ($data as $k => $v) {
            if (!$v['username']) {
                $data[$k]['username'] = '';
            }
            if (!$v['dept_name']) {
                $data[$k]['dept_name'] = '';
            } else {
                $data[$k]['dept_name'] = ' --- ' . $v['dept_name'];
            }
            $data[$k]['classroom'] = $v['classroom']?explode(';', $v['classroom'])[0]:'无';
			$data[$k]['classname'] = $v['classname']?explode(';', $v['classname'])[0]:'无';
        }

        exit(retJson('返回搜索结果', true, $data));
    }



    function get_string_between($string, $start, $end)
    {
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
    // 0507 课程信息session保存
    function setClass()
    {
        $this->load->model("Class_model");
        if (isset($_POST['class_name'])) {
            $_SESSION[SESS_ASSESS]['class_name'] = $_POST['class_name'];
            if (isset($_POST['listen_time'])) {
              $_SESSION[SESS_ASSESS]['listen_time'] = $_POST['listen_time'];
            }
            if (isset($_POST['course_units'])) {
              $_SESSION[SESS_ASSESS]['course_units'] = $_POST['course_units'];
            }
            // $month = date('m', time());
            if ($this->month == 9 || $this->month == 10 || $this->month == 11 || $this->month == 12 || $this->month == 1) {
                if ($this->month == 1) {
                    $year = date('Y', time()) - 1;
                    $year_last = date('Y', time());
                } else {
                    $year = date('Y', time());
                    $year_last = date('Y', time()) + 1;
                }
                // $year=date('Y',time());
                // $year_last=date('Y',time())+1;
                $default_academic_year = strval($year . '-' . $year_last);
            } else if ($this->month == 2 || $this->month == 3 || $this->month == 4 || $this->month == 5 || $this->month == 6 || $this->month == 7 || $this->month == 8) {
                $year = date('Y', time()) - 1;
                $year_last = date('Y', time());
                $default_academic_year = strval($year . '-' . $year_last);
            }
            if($_POST['class_name'] !=""){
                if(isset($_POST['class_id'])){
                    $connar['where'] = $this->sqlLikeEscape('db_class.name like "%??%" and db_class.id=?? and db_class.elective_lesson_no like "%??%"', array($_POST['class_name'],$_POST['class_id'], $default_academic_year));
                }else{
                    $connar['where'] = $this->sqlLikeEscape('db_class.name like "%??%"  and db_class.elective_lesson_no like "%??%"', array($_POST['class_name'], $default_academic_year));
                }
               
                if (isset($_SESSION[SESS_ASSESS]['teacher_code'])) {
                    $connar['where'] .= $this->sqlLikeEscape(' and teacher_id="??"', array($_SESSION[SESS_ASSESS]['teacher_code']));
                }
                $connar['join'] = array('db_admin a', 'a.usercode=db_class.teacher_id');
                $connar['items'] = "db_class.id,db_class.classname,db_class.course_units,db_class.type,db_class.classroom,db_class.teacher_id,a.usercode,a.username,a.dept_name,a.title,a.age_groups,a.teachers_attribute,db_class.code";
                $data = $this->Class_model->get_single_full($connar);

                if ($data) {
                    $_SESSION[SESS_ASSESS]['class_id'] = $data['id'];
                    // $_SESSION[SESS_ASSESS]['classtype']=$data['type'];
                    $_SESSION[SESS_ASSESS]['classname'] = $data['classname'];
                    $_SESSION[SESS_ASSESS]['class_code'] = $data['code'];
                    $_SESSION[SESS_ASSESS]['course_units'] = $data['course_units'];
                    $_SESSION[SESS_ASSESS]['teacher_id'] = $data['teacher_id'];
                    $_SESSION[SESS_ASSESS]['teacher_name'] = $data['username'];
                    $_SESSION[SESS_ASSESS]['teacher_code'] = $data['usercode'];
                    $_SESSION[SESS_ASSESS]['dept_name'] = $data['dept_name'];
                    $_SESSION[SESS_ASSESS]['title'] = $data['title'];
                    $_SESSION[SESS_ASSESS]['age_groups'] = $data['age_groups'];
                    $_SESSION[SESS_ASSESS]['teachers_attribute'] = $data['teachers_attribute'];
                    if ($data['classroom']) {
                        $_SESSION[SESS_ASSESS]['classroom'] = explode(';', $data['classroom'])[0];
                    }
                }
            }
            
            // echo $this->db->last_query();
            // var_dump($data);
           
            exit(retJson('保存成功', true));
        }
        exit(retJson('系统错误', false));
    }

    function changeSksj()
    {
        $this->load->model("Class_model");
        // $month = date('m', time());
        if ($this->month == 9 || $this->month == 10 || $this->month == 11 || $this->month == 12 || $this->month == 1) {
            $year = date('Y', time());
            $year_last = date('Y', time()) + 1;
            $default_academic_year = strval($year . '-' . $year_last);
        } else if ($this->month == 2 || $this->month == 3 || $this->month == 4 || $this->month == 5 || $this->month == 6 || $this->month == 7 || $this->month == 8) {
            $year = date('Y', time()) - 1;
            $year_last = date('Y', time());
            $default_academic_year = strval($year . '-' . $year_last);
        }
        $connar['where'] = $this->sqlLikeEscape('elective_lesson_no like "%??%"', array($default_academic_year));
        $connar['items'] = "name,db_class.id,sksj";
        $batch_sql = '';
        $data = $this->Class_model->get_list_full($connar);
        foreach ($data as $k => $v) {
            $sksj = explode(';', $v['sksj']);
            $sj = '';

            foreach ($sksj as $k1 => $v1) {
                $quantumsw = substr($v1, 9, 1) <= 4 ? '上午' : '下午';
                $weekday = substr($v1, 0, 6);
                $sj .= $weekday . $quantumsw . ";";
            }
            $this->Class_model->update(array('sksj1'=>$sj,'id'=>$v['id']),'id');
            // $batch_sql .= "update db_class set sksj1='" . $sj . "' where id=" . $v['id'] . ";";
        }
        // echo $batch_sql;
        // $this->db->query($batch_sql);
    }
}
